<?php

# http://projects:8081/cdapp-2kt-api/api/hours_load_personal_details.php

// include_once '_portal.php';
header('Content-Type:text/html;charset=GBK');

// $month = $_GET['month'];

const TARGET_SERVER_IP = '172.22.107.22';

# get session_id

# keywords:
# FR.SessionMgr.register('43527', contentPane);

$request_params = '{
  "SEARCHYM": "2022-08",
  "ORGID": 13,
  "USERID2": "20120039,20160039,20140041,20200006,20170100,20200007,20170089,20180066,20100003,20180064,20210052,20140037,20150013,20170074,20130037,20190078,20180056,20200022,20170084,20090009,20120043,20100044,20190054,20110045,20190022,20070043,20130039,20150029,20110048,20180020,20060013,20070020,20100040,20080031,20190018,20170079,20110044,20160034,20210058,20180022,20170090,20120036,20110107,20070024,20200037,20170106,20110047,20200039,20120035,20060012,20190033,20160042,20210043,20130042,20130033,20130038,20180042,20170065,20080034,20200046,20050025,20180075,20200004,20200047,20190066,20210045,20190059,20190067,20130034,20090041,20120040,20190006,20100039"
}';

$session_id = get_session();
// $users = get_users($session_id);
// var_dump($users);
// echo json_encode($users, JSON_UNESCAPED_UNICODE);
// echo '<br /><br />';
// var_dump($users);
send_post($session_id, $request_params);
file_put_contents('./res.html', send_get($session_id));

function get_session() {
  $page_html = file_get_contents('http://' . TARGET_SERVER_IP . '/WebReport/ReportServer?reportlet=%E6%96%B0%E7%89%88_%E7%AE%A1%E5%88%B6%E6%B4%A5%E8%B4%B4%E4%B8%AA%E4%BA%BA%E6%8A%A5%E8%A1%A8.cpt&op=view&userId=19&orgId=4');
  $session_id = null;
  $result = null;
  $rules = "/FR.SessionMgr\.register\(\'(\d*)\', contentPane\);/i";
  preg_match($rules, $page_html, $result);
  $session_id = (int) $result[1];
  return $session_id;
}

function send_post($session_id, $parameters_d) {
  $url = 'http://' . TARGET_SERVER_IP . '/WebReport/ReportServer?op=fr_dialog&cmd=parameters_d&sessionID=' . $session_id;
  $response = '';
  try {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded; charset=utf-8"));
    curl_setopt($curl, CURLOPT_POSTFIELDS, '__parameters__=' . $parameters_d);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($curl);
    // logfile('XML_RESPONSE', $return_xml);
    curl_close($curl);
  } catch (Exception $e) {
    logfile('XML_RESPONSE', $e->getMessage());
  }
  return $response;
}

function send_get($session_id) {
  $url = 'http://' . TARGET_SERVER_IP . '/WebReport/ReportServer?op=fr_view&cmd=view_content&sessionID=' . $session_id . '&reportIndex=0';
  $html = '';
  try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $html = curl_exec($ch);
    curl_close($ch);
    return $html;
  } catch (Exception $e) {
    logfile('XML_RESPONSE', $e->getMessage());
  }
}

function get_users($session_id) {
  $curl = curl_init();
  $url = 'http://' . TARGET_SERVER_IP . '/WebReport/ReportServer?op=widget&widgetname=userId2&sessionID=' . $session_id;
  $params = [
    ['label' => '管制一室', 'value' => 12, 'content' => 'dependence=%7B%22ORGID%22%3A12%7D&startIndex=0&limitIndex=500&reload=true'],
    ['label' => '管制二室', 'value' => 13, 'content' => 'dependence=%7B%22ORGID%22%3A13%7D&startIndex=0&limitIndex=500&reload=true'],
  ];
  $responses = [];
  foreach ($params as $param) {
    curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => $param['content'],
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/x-www-form-urlencoded',
      ),
    ));

    $response = curl_exec($curl);
    curl_close($curl);
    $responses[] = ['label' => $param['label'], 'value' => $param['value'], 'children' => json_decode($response), 'raw' => $response];
  }
  return $responses;
}